<?php if (!defined("MAIN_DIR")) { header("Location: /"); die; }
/**
 * iBugTracker
 *
 * An open source Bug Tracker for PHP 5 or later
 *
 * @package		iBugTracker
 * @author		Anton Minacov
 * @copyright	Copyright (c) 2011, Anton Minacov
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Router Class 
 *
 * Router provide work and manage the entire application
 *
 * @package		iBugTracker
 * @subpackage	Classes
 * @author		Anton Minacov
 * @copyright	Copyright (c) 2011, Anton Minacov
 */

class Router {
	
	private $_path = array();
	private $_GET = array();
	private $_POST = array();
	private $_COOKIE = array();
	public $db;
	public $user;
	
	function __construct(){
		
		session_start();
		
		$this->db = new MySQL(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
		$this->user = new User($this->db);
		
		$request = explode("?",$_SERVER['REQUEST_URI']);
		$dir = trim($request[0],"/");
		// get GET-params
		if (count($_GET)) {
			foreach($_GET as $key=>$value){
				$this->_GET[$key] = $value;
			}
		}
		// get POST-params
		if (count($_POST)){
			foreach($_POST as $key=>$value){
				$this->_POST[$key] = $value;
			}
		}
		
		// get cookie
		if (count($_COOKIE)){
			foreach($_COOKIE as $key=>$value){
				$this->_COOKIE[$key] = $value;
			}
		}
		// build path
		if ($dir == ""){
			array_push($this->_path,"index");
		} else {
			$mas = explode("/",$dir);
			for ($i = 0; $i < count($mas); $i++)
				array_push($this->_path,$mas[$i]);
		}
	}
	
	function go(){
		if (!$this->user->isUser() && $this->getPath() != "auth"){
			$this->to("/auth");
		} else {
			switch($this->getPath()){
				case "index": require_once MAIN.MAINFILE; break;
				case "auth": require_once AUTH.MAINFILE; break;
				case "ajax": require_once AJAX.MAINFILE; break;
				case "admin": require_once ADMIN.MAINFILE; break;
				default: require_once PROJECT.MAINFILE; break;
			}
		}
		$this->db->close();
	}
	
	// direct to page 
	function to($url='/'){
		header("Location: $url");
		exit;
	}
	
	// get path name by number
	public function getPath($number=1){
		$number--;
		if (isset($this->_path[$number])){
			return $this->_path[$number];
		}
		return "index";
	}
	function loadFile($file,$in_subfld=true,$counter=0,$path=""){
		if ($counter < 3){
		
			// for the first time find the path of require file
			if ($path == ""){
				$trace = debug_backtrace();
				$path = dirname($trace[0]['file']);
			}
			
			// if not found - search in subfolder
			if (!is_file($path.DS.$file)){
				if ($in_subfld){
					$this->loadFile($file,true,++$counter,dirname($path));
				} else {
					die("<h1>file not found<h1>");
				}
			// else include this one
			} else {
				require $path.DS.$file;
			}
		} else {
			die("<h1>file not found<h1>");
		}
	}
	public function GET($var,$strip=TRUE,$filter=''){
		$_var = null;
		
		if (isset($this->_GET[$var])){
			switch($filter){
				case "int":		$_var = filter_var($this->_GET[$var],FILTER_VALIDATE_INT); break;
				case "bool":	$_var = filter_var($this->_GET[$var],FILTER_VALIDATE_BOOLEAN); break;
				case "text":	$_var = filter_var($this->_GET[$var],FILTER_SANITIZE_MAGIC_QUOTES); break;
				default:		$_var = $this->_GET[$var];
			}
			
			if ($strip){
				$_var = strip_tags($_var);
			}
		}
		
		return  $_var;
	}
	
	public function POST($var,$strip=TRUE,$filter=''){
		$_var = null;
		
		if (isset($this->_POST[$var])){
			switch($filter){
				case "int":		$_var = filter_var($this->_POST[$var],FILTER_VALIDATE_INT); break;
				case "bool":	$_var = filter_var($this->_POST[$var],FILTER_VALIDATE_BOOLEAN); break;
				case "text":	$_var = filter_var($this->_POST[$var],FILTER_SANITIZE_MAGIC_QUOTES); break;
				default:		$_var = $this->_POST[$var];
			}
			
			if ($strip){
				$_var = strip_tags($_var);
			}
		}
		
		return  $_var;
	}
	
	public function COOKIE($var){
		return isset($this->_COOKIE[$var]) ? $this->_COOKIE[$var] : null;
	}
}
?>